package org.jsola.hr.common;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.ObjectUtil;
import org.jsola.admin.vo.AreaReduceVO;
import org.jsola.common.ArrayKit;
import org.jsola.common.SafeKit;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 格式校验工具类
 *
 * @Author ZDD
 */
public class FormatUtils {
    public static final String BEIJING = "北京";
    public static final String SHANGHAI = "上海";
    public static final String TIANJIN = "天津";
    public static final String CHONGQING = "重庆";

    /**
     * 将省市区转为系统的areaId,无法识别返回null； 示例：山东省/青岛市/市北区
     *
     * @param cityName 省市区地址，支持：省/市/区，省/市 两种格式
     * @param areaList 系统所有的省市区
     * @return s_area表id
     */
    public static String cityNameToAreaCode(String cityName, List<AreaReduceVO> areaList) {
        List<String> cityNameList = ArrayKit.split(cityName, "/");
        if (CollectionUtils.isEmpty(cityNameList)) {
            return null;
        }
        String areaId;
        // 只有省市
        if (cityNameList.size() == 2) {
            areaId = checkCity(cityName, areaList);
        } else {
            areaId = checkCity1(cityName, areaList);
        }
        return areaId;
    }

    /**
     * 校验省市是否存在，只支持 省/市 格式
     */
    public static String checkCity(String cityName, List<AreaReduceVO> areaList) {
        if (StringUtils.isEmpty(cityName)) {
            return null;
        }
        Map<Integer, List<AreaReduceVO>> listMap = areaList.stream().collect(Collectors.groupingBy(AreaReduceVO::getLevel));
        String cityName1 = cityName.replace("省", "").replace("市", "");
        String[] split = cityName1.split("/");
        if (split.length == 1) {
            List<AreaReduceVO> collect = listMap.get(1).stream().filter(p -> p.getName().equals(cityName)).collect(Collectors.toList());
            if (CollectionUtils.isEmpty(collect)) {
                return null;
            } else {
                return collect.get(0).getId().toString();
            }
        } else if (split.length == 2) {
            //市
            List<AreaReduceVO> collect = listMap.get(2).stream().filter(p -> split[1].equals(p.getName())).collect(Collectors.toList());
            if (CollectionUtils.isEmpty(collect)) {
                return null;
            } else {
                return collect.get(0).getId().toString();
            }
        }
        return null;
    }

    /**
     * 校验省市是否存在，支持 省/市/区 格式
     */
    public static String checkCity1(String cityName, List<AreaReduceVO> areaList) {
        if (StringUtils.isEmpty(cityName)) {
            return null;
        }
        Map<Integer, List<AreaReduceVO>> listMap = areaList.stream().collect(Collectors.groupingBy(AreaReduceVO::getLevel));
        String[] split = cityName.split("/");
        if (split.length == 2) {
            List<AreaReduceVO> collect = listMap.get(3).stream().filter(p -> p.getName().equals(split[1])).collect(Collectors.toList());
            if (CollectionUtils.isEmpty(collect)) {
                return null;
            } else {
                return collect.get(0).getId().toString();
            }
        } else if (split.length == 3) {
            List<AreaReduceVO> collect = listMap.get(3).stream().filter(p -> split[2].equals(p.getName())).collect(Collectors.toList());
            if (CollectionUtils.isEmpty(collect)) {
                return null;
            } else {
                return collect.get(0).getId().toString();
            }
        }
        return null;
    }

    /**
     * 根据code数字获取城市完整地址
     *
     * @param content  数字
     * @param areaList 城市数据
     * @param split    数字连接符
     * @param append   返回城市连接符
     * @return 城市名
     */
    public static String getNameByCode(String content, List<AreaReduceVO> areaList, String split, String append, String cityName) {
        if (StringUtils.isEmpty(content)) {
            return null;
        }
        String replaceResult = content.replace(split, "");
        if (!Validator.isNumber(replaceResult)) {
            return null;
        }
        Map<Long, AreaReduceVO> idObjMap = areaList.parallelStream().collect(Collectors.toMap(AreaReduceVO::getId, p -> p));
        String[] splitResult = content.split(split);
        if (idObjMap.containsKey(SafeKit.getLong(splitResult[splitResult.length - 1]))) {
            AreaReduceVO areaReduceVO = idObjMap.get(SafeKit.getLong(splitResult[splitResult.length - 1]));
            if (ObjectUtil.isNotNull(areaReduceVO)) {
                if (StringUtils.isEmpty(cityName)) {
                    cityName = areaReduceVO.getName();
                } else {
                    cityName = areaReduceVO.getName() + append + cityName;
                }
                if (areaReduceVO.getParentId() != 0) {
                    cityName = getNameByCode(SafeKit.getString(areaReduceVO.getParentId()), areaList, split, append, cityName);
                }
            }
        }
        return cityName;
    }

    /**
     * 城市名转code（最后一级）
     *
     * @param cityName 名字
     * @param areaList 所有地址
     * @param isFirst  如果地址查到多个是否取level低的
     * @param splits   分割文本
     * @return 地址code
     */
    public static String checkCity1(String cityName, List<AreaReduceVO> areaList, Boolean isFirst, String... splits) {
        if (StringUtils.isEmpty(cityName)) {
            return null;
        }
        String splitStr = "";
        for (String sp : splits) {
            if (cityName.contains(sp)) {
                splitStr = sp;
            }
        }
        String[] split = cityName.split(splitStr);
        if (split.length == 0) {
            return null;
        }
        Map<Integer, List<AreaReduceVO>> levelAreaMap = areaList.stream().collect(Collectors.groupingBy(AreaReduceVO::getLevel));
        List<AreaReduceVO> areaReduceVOList = areaList.parallelStream().filter(areaReduceVO -> areaReduceVO.getName().equals(split[split.length - 1])).collect(Collectors.toList());
        if (areaReduceVOList.size() == 0) {
            return null;
        } else if (areaReduceVOList.size() == 1) {
            return SafeKit.getString(areaReduceVOList.get(0).getId());
        } else {
            if (split.length == 1) {
                areaReduceVOList = ListUtil.sortByProperty(areaReduceVOList, "level");
                if (isFirst) {
                    return SafeKit.getString(areaReduceVOList.get(0).getId());
                } else {
                    return SafeKit.getString(areaReduceVOList.get(areaReduceVOList.size() - 1).getId());
                }
            } else {
                for (AreaReduceVO areaReduceVO : areaReduceVOList) {
                    if (areaReduceVO.getName().equals(split[split.length - 2])) {
                        return SafeKit.getString(areaReduceVO.getId());
                    }
                }
            }
        }
        return null;
    }
}
